home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / batchut / dpv12.zip / DPV.C next >
C/C++ Source or Header  |  1990-12-08  |  8KB  |  218 lines

  1. /****************************************************************************/
  2. /* PROGRAM:  dpv (Drive Path Volume)                                        */
  3. /* VERSION:  1.2                                                            */
  4. /*    DATE:  December 8, 1990                                               */
  5. /*  AUTHOR:  Sydney M. Willett                                              */
  6. /* DESCRIPTION:  Checks if a formatted disk is inserted in a drive, if a    */
  7. /*               drive or path is valid, or if a volume label matches.      */
  8. /*               Returns an errorlevel upon exit to indicate the status.    */
  9. /* INPUTS:  DPV [drive:][path] [/Vvolume]                                   */
  10. /*              drive:    drive                                             */
  11. /*              path:     path for search - preceding \ searches from root  */
  12. /*              /Vvolume  volume label for search                           */
  13. /* OUTPUTS:  Returns an errorlevel, each value indicating:                  */
  14. /*              0 = disk in drive okay -or- subdirectory exists             */
  15. /*                  -or- volume label matches                               */
  16. /*              1 = volume label doesn't match                              */
  17. /*              2 = invalid path                                            */
  18. /*              4 = invalid drive                                           */
  19. /*              5 = no diskette in drive                                    */
  20. /*              6 = unformatted diskette in drive                           */
  21. /*              7 = invalid parameter(s)                                    */
  22. /****************************************************************************/
  23.  
  24.  
  25. /*============================================================================
  26. INCLUDES
  27. ----------------------------------------------------------------------------*/
  28. #include <dir.h>
  29. #include <dos.h>
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33.  
  34.  
  35. /*============================================================================
  36. EQUATES
  37. ----------------------------------------------------------------------------*/
  38. #define BACKSLASH 92
  39.  
  40.  
  41. /*============================================================================
  42. DATA DECLARATIONS
  43. ----------------------------------------------------------------------------*/
  44. typedef unsigned char BYTE;
  45.  
  46.  
  47. /*============================================================================
  48. FUNCTION DECLARATIONS
  49. ----------------------------------------------------------------------------*/
  50. void volcat(char *vol,char *str);
  51.  
  52.  
  53. /*============================================================================
  54. MAIN
  55. ----------------------------------------------------------------------------*/
  56. void main(int argc,char **argv)
  57. {
  58. BYTE drive;
  59. char *buffer;
  60. char chk_drive = 0;
  61. char chk_path = 0;
  62. char chk_vol = 0;
  63. char path[81];
  64. char volume[16];
  65. int arg;
  66. int argndx;
  67. int errorlevel = 7; /*  0 = disk in drive okay            */
  68.                     /*      -or- subdirectory exists      */
  69.                     /*      -or- volume label matches     */
  70.                     /*  1 = volume label doesn't match    */
  71.                     /*  2 = invalid path                  */
  72.                     /*  4 = invalid drive                 */
  73.                     /*  5 = no diskette in drive          */
  74.                     /*  6 = unformatted diskette in drive */
  75.                     /*  7 = invalid parameter(s)          */
  76. int i;
  77. struct ffblk fblk;
  78. struct SREGS sregs;
  79. union REGS regs;
  80.  
  81.   /* ... Get current drive and directory, and setup strings */
  82.   getcwd(path,80);
  83.   i = strlen(path);
  84.   if ( path[i-1] != BACKSLASH ) /* need trailing '\' */
  85.   {
  86.     path[i] = BACKSLASH;
  87.     path[i+1] = NULL;
  88.   }
  89.   strncpy(volume,path,3);
  90.   volume[3] = NULL;
  91.   drive = path[0] - 'A';
  92.  
  93.   /* ... Parse command line arguments ... */
  94.   for ( arg=1 ; arg<argc ; arg++ )
  95.   {
  96.     strupr(argv[arg]);
  97.     if ( chk_vol ) /* next part of volume label, append to volume string */
  98.     {
  99.       volcat(&volume[3]," "); /* add space */
  100.       volcat(&volume[3],argv[arg]);
  101.     }
  102.     else
  103.     {
  104.       argndx = 0;
  105.       if ( argv[arg][1] == ':' ) /* drive specified */
  106.       {
  107.         argndx = 2;
  108.         if ( argv[arg][0] != path[0] ) /* not current drive */
  109.         {
  110.           drive = argv[arg][0] - 'A';
  111.           path[0] = argv[arg][0];
  112.           volume[0] = argv[arg][0];
  113.           chk_drive++;
  114.         }
  115.       }
  116.       switch ( argv[arg][argndx] )
  117.       {
  118.         case NULL      : errorlevel = 0; /* drive only */
  119.                          break;
  120.         case '-'       :
  121.         case '/'       : switch ( argv[arg][1] ) /* volume label */
  122.                          {
  123.                            case 'V' : volcat(&volume[3],&argv[arg][2]);
  124.                                       chk_vol++;
  125.                                       errorlevel = 0;
  126.                                       break;
  127.                            default  : break; /* invalid argument */
  128.                          }
  129.                          break;
  130.         case BACKSLASH : path[2] = NULL; /* full path, shorten path string */
  131.         default        : strcat(path,&argv[arg][argndx]); /* append arg */
  132.                          if ( strlen(path) > 3 ) /* if not root directory, */
  133.                            chk_path++;                       /* check path */
  134.                          errorlevel = 0;
  135.                          break;
  136.       }
  137.     }
  138.   }
  139.  
  140.   /* ... Perform actions, if any ... */
  141.   if ( errorlevel == 7 ) /* invalid or no arguments */
  142.   {
  143.     printf("DPV 1.2  ▒▒▒ Copyright 1990 by S.M. Willett ▒▒▒\n\n");
  144.     printf("Usage: DPV [drive:][path] [/Vvolume]\n");
  145.     printf("           drive:     drive\n");
  146.     printf("           path:      path (preceding \\ searches from root)\n");
  147.     printf("           /Vvolume   volume label\n\n");
  148.     printf("Example:  DPV a:\\work /Vwordstar\n");
  149.     printf("  This searches drive A: for directory named \\WORK and volume label WORDSTAR\n\n");
  150.   }
  151.   else
  152.   {
  153.     if ( chk_drive ) /* check drive */
  154.     {
  155.       buffer = malloc(8192); /* set up dummy buffer area */
  156.       regs.h.al = drive; /* set up regs for int 25h, absolute sector read */
  157.       regs.x.cx = 1;
  158.       regs.x.dx = 1;
  159.       sregs.ds = FP_SEG(buffer);
  160.       regs.x.bx = FP_OFF(buffer);
  161.       int86x(0x25,®s,®s,&sregs); /* absolute sector read */
  162.       free(buffer); /* release buffer memory */
  163.       if ( regs.x.cflag ) /* error reading disk */
  164.       {
  165.         switch ( regs.x.ax ) /* evalute disk read */
  166.         {
  167.           case 0x020C :
  168.           case 0x040C :
  169.           case 0x100C : errorlevel = 6; /* unformatted disk */
  170.                         break;
  171.           case 0x8002 : errorlevel = 5; /* no diskette in drive */
  172.                         break;
  173.           default     : errorlevel = 4; /* invalid drive */
  174.                         break;
  175.         }
  176.       }
  177.     }
  178.     if ( !errorlevel )
  179.     {
  180.       if ( chk_path ) /* search for subdirectory */
  181.       {
  182.         if ( findfirst(path,&fblk,FA_DIREC) != 0 ) /* if unsuccessful, */
  183.           errorlevel |= 2;               /* subdirectory doesn't exist */
  184.       }
  185.       if ( chk_vol ) /* search for volume label */
  186.       {
  187.         for ( i=strlen(volume) ; i>10 ; i--) /* insert '.' if needed */
  188.         {             
  189.           volume[i+1] = volume[i];
  190.           if ( i == 11 )
  191.             volume[11] = '.';
  192.         }
  193.         if ( findfirst(volume,&fblk,FA_LABEL) != 0 ) /* if unsuccessful, */
  194.           errorlevel |= 1;                      /* not same volume label */
  195.       }
  196.     }
  197.   }
  198.  
  199.   /* ... Return errorlevel ... */
  200.   exit(errorlevel);
  201. }
  202.  
  203.  
  204. /*============================================================================
  205. VOLCAT
  206. ----------------------------------------------------------------------------*/
  207. void volcat(char *vol,char *str)
  208. {
  209. int remaining;
  210.  
  211.   remaining = 11 - strlen(vol);
  212.   if ( remaining > 0 )
  213.     strncat(vol,str,remaining);
  214. }
  215.  
  216.  
  217.  
  218.